package top.sssd.auth.service.impl.rolePermission;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.sssd.auth.service.rolePermission.RolePermissionService;
import top.sssd.auth.mapper.rolePermission.RolePermissionMapper;
import top.sssd.auth.model.rolePermission.converter.RolePermissionConverter;
import top.sssd.auth.model.rolePermission.entity.RolePermission;
import top.sssd.auth.model.rolePermission.request.RolePermissionQueryReq;
import top.sssd.auth.model.rolePermission.request.RolePermissionSaveOrUpdateReq;
import top.sssd.auth.model.rolePermission.response.RolePermissionResp;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Override
    public Page<RolePermissionResp> queryPage(RolePermissionQueryReq rolepermissionQueryReq) {
        Page<RolePermission> page = new Page<>(rolepermissionQueryReq.getPageNum(), rolepermissionQueryReq.getPageSize());
        Page<RolePermission> resultPage = commonQuery(rolepermissionQueryReq).page(page);
        List<RolePermissionResp> resultList = resultPage.getRecords().parallelStream()
                .map(RolePermissionConverter.INSTANCE::entityToResp)
                .toList();
        return new Page<RolePermissionResp>()
                .setTotal(resultPage.getTotal())
                .setCurrent(resultPage.getCurrent())
                .setSize(resultPage.getSize())
                .setRecords(resultList);
    }

    @Override
    public List<RolePermissionResp> queryRolePermissionRespListByRolePermissionReq(RolePermissionQueryReq RolePermissionQueryReq) {
        List<RolePermission> list = commonQuery(RolePermissionQueryReq).list();
        return list.parallelStream()
                .map(RolePermissionConverter.INSTANCE::entityToResp)
                .toList();
    }

    private LambdaQueryChainWrapper<RolePermission> commonQuery(RolePermissionQueryReq rolepermissionQueryReq) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(rolepermissionQueryReq.getId()), RolePermission::getId, rolepermissionQueryReq.getId())
                .eq(ObjectUtil.isNotEmpty(rolepermissionQueryReq.getRoleId()), RolePermission::getRoleId, rolepermissionQueryReq.getRoleId())
                .eq(ObjectUtil.isNotEmpty(rolepermissionQueryReq.getPermissionId()), RolePermission::getPermissionId, rolepermissionQueryReq.getPermissionId())
                .orderByDesc(RolePermission::getUpdatedTime);
    }

    @Override
    public RolePermissionResp getRolePermissionRespById(Serializable id) {
        RolePermission rolepermission = this.getById(id);
        if (Objects.nonNull(rolepermission)) {
            return RolePermissionConverter.INSTANCE.entityToResp(rolepermission);
        }
        throw new RuntimeException("角色权限关联表不存在");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean saveRolePermissionReq(RolePermissionSaveOrUpdateReq rolepermissionSaveOrUpdateReq) {
        RolePermission rolepermission = RolePermissionConverter.INSTANCE.saveOrUpdateRolePermissionReqToEntity(rolepermissionSaveOrUpdateReq);
        return this.save(rolepermission);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateRolePermissionReq(RolePermissionSaveOrUpdateReq rolepermissionSaveOrUpdateReq) {
        RolePermission rolepermission = RolePermissionConverter.INSTANCE.saveOrUpdateRolePermissionReqToEntity(rolepermissionSaveOrUpdateReq);
        return this.updateById(rolepermission);
    }
}

